package com.iseastar.guojiang;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.baidu.mapapi.SDKInitializer;
import com.iseastar.guojiang.app.AppCache;
import com.iseastar.guojiang.consts.AppConfig;
import com.iseastar.guojiang.logic.AppHttp;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;
import com.umeng.analytics.MobclickAgent;

import java.lang.Thread.UncaughtExceptionHandler;

import droid.frame.App;
import droid.frame.push.PushMsgID;
import droid.frame.utils.CrashHandler;
import droid.frame.utils.android.Log;

/**
 * @author coffee<br>
 * 2015-3-23下午8:23:44
 */
@SuppressLint("HandlerLeak")
public class BeeApplication extends App {

    private CrashHandler crashHandler;
    private RefWatcher refWatcher;

    public static RefWatcher getRefWatcher(Context context) {
        BeeApplication application = (BeeApplication) context.getApplicationContext();
        return application.refWatcher;
    }

    public UncaughtExceptionHandler getUncaughtExceptionHandler() {
        return crashHandler;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        crashHandler = new CrashHandler(getContext());
        Thread.setDefaultUncaughtExceptionHandler(crashHandler);
        //
        AppConfig.init();
        // 百度地图---在使用 SDK 各组间之前初始化 context 信息，传入 ApplicationContext
        SDKInitializer.initialize(getApplicationContext());
        // // umeng禁止默认的页面统计
        // MobclickAgent.openActivityDurationTrack(false);
        // // 发送策略
        // MobclickAgent.updateOnlineConfig(this);
        MobclickAgent.setCatchUncaughtExceptions(true);
        refWatcher = LeakCanary.install(this);
        // StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()//
        // .detectAll().penaltyLog().build());
        // // StrictMode VmPolicy violation with POLICY_DEATH; shutting down. 百度地图
        // StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyLog().build());
    }

    protected void onCreateHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case PushMsgID.message_recv:
                        Log.d("44444", "推送过来接收到的");
                        if (AppCache.getUser() == null || AppCache.getUser().getId() == null) {
                            return;
                        }
//                        AppLogic.courierBackToHomeAndRefresh(getContext());
//                        PushMessageModel item = JsonParser.parse(msg.obj + "", PushMessageModel.class);
                        Log.d("jpush", "-----------jpush-------" + msg.obj);
//                        if (item != null) {
//                            Class<?> activityClass = null;
//                            switch (item.getType()) {
//                                //邮票即将过期
//                                case 1:
//                                    if (activityClass == null) {
//                                        activityClass = CouponListActivity.class;
//                                    }
//                                case 8:
//                                    if (activityClass == null) {
//                                        activityClass = ParcelDetailActivity.class;
//                                    }
//                                    //						case 10:// 包裹详情
//                                    //							activityClass = ParcelDetailActivity.class;
//                                case 11:// 服务评价
//                                    if (activityClass == null) {
//                                        activityClass = EvaluateActivity.class;
//                                    }
//                                case 12:
//                                    if (activityClass == null) {
//                                        activityClass = OrderPayActivity.class;
//                                    }
//                                    Intent intent = new Intent();
//                                    intent.setClass(getContext(), activityClass);
//                                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                                    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);// 启动模式不一样
//                                    Bundle bundle = new Bundle();
//                                    bundle.putString("orderId", item.getOrderId());
//                                    bundle.putString("parcelId", item.getParcelId() + "");
//                                    intent.putExtra("parcelBundle", bundle);
//                                    sendNotification(getContext(), item.getDescription(), intent);
//                                    break;
//                                default:
//                                    sendNotification(getContext(), item.getDescription(), null);
//                                    break;
//                            }
//
//                        }
                        break;
                    case PushMsgID.message_token:
                        String token = (String) msg.obj;
                        if (AppCache.getUser() != null && AppCache.getUser().getId() != null) {
                            AppHttp.getInstance().updatePushToken(token);
                        }
                        break;
                    default:
                        break;
                }
            }
        };
    }

    private void startActivity(Class<?> cls, int orderId, String description) {
        Intent tmp = new Intent(getContext(), cls);
        tmp.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        tmp.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        tmp.putExtra("orderId", orderId);
        if ((!TextUtils.isEmpty(description)) && (!description.equals("null"))) {
            tmp.putExtra("description", description);
        }
        startActivity(tmp);
    }

}
